package myJava.Web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

//https://www.oschina.net/question/147939_140523
public class AnalyzeWeb {
	
	public static int count = 0;
	public static void main(String args[]) {
		//运行
		run();
	}
	
	public static void run() {
		WebInfo twi = null;
		String temp;
		//获取当前时间年月日
		
		//一年的每日一天
		
		for (int i = 1; i < Time.nowDay.getMonth(); i++) {
			String tempDay = Time.nowDay.getYear()+"";
			if (i < 10) {
				tempDay = tempDay + "-0" + i;
			}else {
				tempDay = tempDay + "-" + i;
			}
			int endDay = WEBUtil.getMonthend(Time.nowDay.getYear(), i);
			for (int j = 1; j <= endDay; j++) {
				temp = tempDay;
				if (j < 10) {
					temp += "-0";
					temp += j;
				}else {
					temp += "-";
					temp += j;
				}
				twi = getContent(temp);
				//twi.printAll();
				if (WEBUtil.downloadImage(twi.getPictureUrl(), twi.getImageFileName()) != -1) count++;	
				SavaWebInfoArray.arrayList.add(twi);
			}
		}
		for (int i = 1; i <= Time.nowDay.getDay(); i++) {
			if (i < 10) {
				temp = "0";
				temp += i;
			}else {
				temp = "";
				temp += i;
			}
			if (Time.nowDay.getMonth() < 10) {
				temp = Time.nowDay.getYear() + "-0" + Time.nowDay.getMonth() + "-" + temp;
			}else {
				temp = Time.nowDay.getYear() + "-" + Time.nowDay.getMonth() + "-" + temp;
			}
			twi = getContent(temp);
			//twi.printAll();
			if (WEBUtil.downloadImage(twi.getPictureUrl(), twi.getImageFileName()) != -1) count++;
			SavaWebInfoArray.arrayList.add(twi);
		}
		saveFileWebInfo(SavaWebInfoArray.arrayList);
		
		System.out.println("imageCount: " + count);
	}
	
	
	public static WebInfo getContent(String day) {
		WebInfo twi = new WebInfo();//获取的信息封装成一个对象
		twi.setDay(day);
		String urlTemp = "";
		//解析目标网址
		final String url = "http://sentence.iciba.com/index.php?&c=dailysentence&m=getdetail&title=";
		urlTemp = url + day;
		
		String webContent = WEBUtil.getWebTxt(urlTemp);
		//System.out.println(webContent);
		
		String temp;
		String pattern;
		
		//content
		pattern = "content.*note";
		temp = WEBUtil.takeWebDetail(webContent, pattern);
		temp = temp.substring(10, temp.length() - 7);
		twi.setContent(temp);
		
		//note
		pattern = "note.*translation";
		temp = WEBUtil.takeWebDetail(webContent, pattern);
		temp = temp.substring(7, temp.length() - 14);
		temp = WEBUtil.stringToChar(temp);
		twi.setNote(temp);
		
		//translation
		pattern = "translation.*picture\":";
		temp = WEBUtil.takeWebDetail(webContent, pattern);
		temp = temp.substring(14, temp.length() - 12);
		temp = WEBUtil.stringToChar(temp);
		twi.setTranslation(temp);
		
		
		
		//pictureUrl
		pattern = "picture2.*picture3\":";
		temp = WEBUtil.takeWebDetail(webContent, pattern);
		temp = temp.substring(11, temp.length() - 13);
		temp = WEBUtil.deleteChar(temp, '\\');
		twi.setPictureUrl(temp);
		
//		temp = WEBUtil.stringToChar(twi.getTranslation());
//		System.out.println(temp);
		
		temp = WEBUtil.takeWebDetail(temp, "big.*.jpg");//获取图片文件名
		twi.setImageFileName(WEBUtil.changeChar(temp, '-'));
		return twi;
	}
	
	public static void saveFileWebInfo(ArrayList<WebInfo> arrayList) {
		try {
			File file = new File("src/myJava/Web/content/content.WebInfo");
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(arrayList);
			oos.close();
			fos.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public static ArrayList<WebInfo> takeFileWebInfo() {
		ArrayList<WebInfo> arrayList = null;
		
		try {
			File file = new File("src/myJava/Web/content/content.WebInfo");
			FileInputStream fis = new FileInputStream(file);
			
			ObjectInputStream ois = new ObjectInputStream(fis);
			Object obj = null;
	        try {
	        	arrayList = (ArrayList<WebInfo>) ois.readObject();
				ois.close();
				fis.close();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return arrayList;
	}
}
